package com.xd.core.debt.controller;


import com.github.pagehelper.Page;
import com.xd.common.constant.SysConstant;
import com.xd.common.util.SecurityUtil;
import com.xd.core.debt.dto.*;
import com.xd.core.debt.service.IDebtService;
import com.xd.core.debt.vo.*;
import com.xd.core.mission.entity.MissionCenterInfo;
import com.xd.core.mission.mapper.MissionCenterInfoMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * <p>
 * 债权管理对应表 前端控制器
 * </p>
 *
 * @author ZCH
 * @since 2025-02-26
 */
@Api(tags = "债权管理")
@RestController
@RequestMapping("/core/debt")
@Slf4j
public class DebtController {

    @Autowired
    private IDebtService debtService;
    @Autowired
    private MissionCenterInfoMapper missionCenterInfoMapper;


    @PreAuthorize("hasAnyAuthority('sys:DebtManagement-viewDeatils')")
    @PostMapping("/debtList")
    @ApiOperation("债权列表分页查询")
    public Page<DebtPageVo> getDebtList(@RequestBody DebtQuery debtQuery) {
        return debtService.getDebtList(debtQuery);
    }

    @PostMapping("/getDebtDetail")
    @ApiOperation("债权信息详情")
    public DebtDetailVo getDebtOne(@RequestBody DebtOneDto dto) {
        return debtService.getDebtOne(dto);
    }

    @PostMapping("/getBatchDebt")
    @ApiOperation("债权转让数据回显")
    public DebtAndPreDepositPayOrderVo getBatchDebt(@RequestBody GetBatchDebtDto batchDebtDto) {
        return debtService.getBatchDebt(batchDebtDto);
    }

    @PreAuthorize("hasAnyAuthority('sys:DebtManagement-BulkTransfer','sys:DebtManagement-ClaimAssignment'" +
            ",'sys:Lending-AssignmentClaim','sys:GuaranteeDetail-AssignmentClaims','sys:InstallmentAssets-debtTransferClick')")
    @PostMapping("/debt/trans")
    @ApiOperation("债权转让")
    public void debtTrans(@RequestBody DebtTansDto debtTansDto) {
        debtService.debtTrans(debtTansDto);
        log.info("转让成功");
    }

    @GetMapping("/getEarlyRepayment/{id}")
    @ApiOperation("提前还款数据回显")
    public EarlyRepaymentVo getEarlyRepaymentQuery(@PathVariable Integer id) {
        return debtService.earlyRepaymentQuery(id);
    }

    @PreAuthorize("hasAnyAuthority('sys:DebtManagement-Prepayment'," +
            "'sys:DebtManagement-EarlySettlement'," +
            "'sys:RepaymentManagement-OfflineRepayment'" +
            ",'sys:Lending-EarlyRepayment')")
    @PostMapping("/earlyRepayment")
    @ApiOperation("提前还款")
    public void earlyRepayment(@RequestBody EarlyRepaymentDto dto) {
        debtService.earlyRepayment(dto,null,"0");
        log.info("提前还款成功");
    }

    @PreAuthorize("hasAnyAuthority('sys:DebtManagement-EarlyRelease','sys:DebtManagement-ExecutionApplication','sys:InstallmentAssets-earlyReleaseDebt')")
    @PostMapping("/applyCertificateOrEarlyReleased")
    @ApiOperation("申请公证书或提前释放债权")
    public void applyCertificate(@RequestBody ApplyCertificateDto dto) {
        debtService.applyCertificate(dto);
        log.info("申请公证书成功或提前释放债权成功");
    }

    @PostMapping("/changeIsWithhold")
    @ApiOperation("修改代收开关状态")
    public void changeIsWithhold(@RequestBody ChangeIsWithholdDto dto) {
        debtService.changeIsWithhold(dto);
    }

    @PreAuthorize("hasAnyAuthority('sys:DebtManagement-Export')")
    @ApiOperation("债权列表导出")
    @PostMapping("/exportDebtList")
    public void exportDebtList(@RequestBody DebtQuery debtQuery) {
        //插入数据状态是正在导出
        MissionCenterInfo missionCenterInfo = new MissionCenterInfo();
        missionCenterInfo.setCompanyId(SecurityUtil.getLoginCompanyId());
        missionCenterInfo.setFileStatus(SysConstant.YSZT_FILE_STATUS_ONE);
        missionCenterInfo.setCreateTime(new Date());
        missionCenterInfo.setUserId(SecurityUtil.getLoginUserId());
        missionCenterInfoMapper.insert(missionCenterInfo);
        //获取数据
        debtService.exportDebtInfoList(debtQuery, missionCenterInfo.getId());
    }


    @PostMapping("/installmentDebtList")
    @ApiOperation("分期债权分页查询")
    public Page<InstallmentDebtPageVo> getInstallmentDebtList(@RequestBody InstallmentDebtQuery query) {
        return debtService.getInstallmentDebtList(query);
    }

    @GetMapping("/getRefundApplication/{incomingId}")
    @ApiOperation("退保申请数据回显")
    public RefundApplicationVo getRefundApplication(@PathVariable Integer incomingId) {
        return debtService.getRefundApplication(incomingId);
    }

    @PreAuthorize("hasAnyAuthority('sys:InstallmentAssets-surrender')")
    @PostMapping("/applySurrender")
    @ApiOperation("退保申请")
    public void applySurrender(@RequestBody ApplySurrenderDto dto) {
        debtService.applySurrender(dto);
        log.info("退保申请成功");
    }


    @PreAuthorize("hasAnyAuthority('sys:InstallmentAssets-Export')")
    @ApiOperation("分期资产导出")
    @PostMapping("/exportGuaranteeSlipList")
    public void exportGuaranteeSlipList(@RequestBody InstallmentDebtQuery query) {
        //插入数据状态是正在导出
        MissionCenterInfo missionCenterInfo = new MissionCenterInfo();
        missionCenterInfo.setCompanyId(SecurityUtil.getLoginCompanyId());
        missionCenterInfo.setFileStatus(SysConstant.YSZT_FILE_STATUS_ONE);
        missionCenterInfo.setCreateTime(new Date());
        missionCenterInfo.setUserId(SecurityUtil.getLoginUserId());
        missionCenterInfoMapper.insert(missionCenterInfo);
        //获取数据
        debtService.exportGuaranteeSlipList(query, missionCenterInfo.getId());
    }
}
